﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Web.UI.WebControls;
using System.Xml;

namespace GKQC.Common
{
    public class Settings
    {
        public static void GetPortBuad(SerialPort sp, MachineType machineType)
        {
            XmlNode node = null;
            try
            {
                if(machineType == MachineType.Caliper)
                    node = Settings.GetCaliperComXmlNode();
                else if(machineType == MachineType.LCR)
                    node = Settings.GetLcrComXmlNode();
                else if (machineType == MachineType.LC)
                    node = Settings.GetLcComXmlNode();
                else if (machineType == MachineType.Lc10)
                    node = Settings.GetLc10ComXmlNode();
                else if (machineType == MachineType.Box)
                    node = Settings.GetBoxComXmlNode();
                else if (machineType == MachineType.Box2)
                    node = Settings.GetBoxCom2XmlNode();

                if (node != null)
                {
                    sp.PortName = node.SelectSingleNode("ComName").InnerText;

                    int baudRate = 9600;
                    int.TryParse(node.SelectSingleNode("BaudRate").InnerText, out baudRate);
                    sp.BaudRate = baudRate;

                    int dataBits = 8;
                    int.TryParse(node.SelectSingleNode("DataBits").InnerText, out dataBits);
                    sp.DataBits = dataBits;

                    string stopBitsString = node.SelectSingleNode("StopBits").InnerText;
                    System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.Two;
                    if (stopBitsString == "1") stopBits = System.IO.Ports.StopBits.One;
                    else if (stopBitsString == "1.5") stopBits = System.IO.Ports.StopBits.OnePointFive;
                    else if (stopBitsString == "2") stopBits = System.IO.Ports.StopBits.Two;
                    else stopBits = System.IO.Ports.StopBits.None;
                    sp.StopBits = stopBits;

                    Parity parity = Parity.None;
                    string parityString = node.SelectSingleNode("Parity").InnerText;
                    if (parityString == "无") parity = Parity.None;
                    else if (parityString == "奇校验") parity = Parity.Odd;
                    else if (parityString == "偶校验") parity = Parity.Even;
                    sp.Parity = parity;
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        /// <summary>
        /// 对象保存为文件，文件名稱儀包含路徑
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="fileName">不包含路径</param>
        public static void SaveObject(object obj, string fileName)
        {
            IFormatter formatter = new BinaryFormatter();
            string path = System.Environment.CurrentDirectory;
            string file = fileName;
            if(!fileName.Contains("\\")) file = path + "\\" + fileName;
            using (Stream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, obj);
                stream.Flush();
                stream.Close();
                stream.Dispose();
            }
        }

        /// <summary>
        /// 根據文件路徑名稱获取的文件对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T GetDataObjectFile<T>(string fileName)
        {
            //string path = System.Environment.CurrentDirectory;
            //string file = path + "\\" + fileName;
            IFormatter formatter = new BinaryFormatter();
            if (File.Exists(fileName))
            {
                using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    T t = (T)formatter.Deserialize(stream);
                    stream.Flush();
                    stream.Close();
                    stream.Dispose();
                    return t;
                }
            }

            return default(T);
        }

        public static XmlDocument _XmlDocument = null;

        public static XmlDocument GeteXmlDoc()
        {
            if (_XmlDocument != null)
            {
                return _XmlDocument;
            }
            else
            {
                string file = GetSettingFileName();
                if (!File.Exists(file))
                {
                    CreateSettingForlder();
                }
                else
                {
                    _XmlDocument = new XmlDocument();
                    _XmlDocument.Load(file);
                }
            }

            return _XmlDocument;
        }


        #region 配置文件XML
        static void CreateSettingForlder()
        {
            string path = GetSettingPath();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static void CreateDataForlder()
        {
            string path = GetDataPath();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static string GetSettingPath()
        {
            return Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory//System.Environment.CurrentDirectory
                , "Settings");
        }

        /// <summary>
        /// 获取当前程序目录下的文件夹Temp路径
        /// </summary>
        /// <returns></returns>
        public static string GetTempPath()
        {
            string temp = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory//System.Environment.CurrentDirectory
                    , "Temp");
            if (!Directory.Exists(temp))
            {
                Directory.CreateDirectory(temp);
            }
            return temp;
        }

        /// <summary>
        /// 获取当前程序目录下的文件夹Temp下的文件
        /// </summary>
        public static void ClearTempFiles()
        {
            try
            {
                string tempPath = GetTempPath();
                if (!Directory.Exists(tempPath)) return;
                string[] files = Directory.GetFiles(tempPath);
                if (files != null && files.Length > 0)
                {
                    files.ToList().ForEach((f) =>
                    {
                        try { File.Delete(f); }
                        catch { }
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        public static string GetDataPath()
        {
            string s = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory//System.Environment.CurrentDirectory
                , "Data");
            if (!Directory.Exists(s))
            {
                Directory.CreateDirectory(s);
            }
            return s;
        }

        public static string GetLocalUpdatePath()
        {
            string s = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory//System.Environment.CurrentDirectory
                , "Update");
            if (!Directory.Exists(s))
            {
                Directory.CreateDirectory(s);
            }
            return s;
        }

        public static void CreateSettingFile()
        {
            CreateSettingForlder();

            string file = GetSettingFileName();
            XmlDocument xmlDoc = new XmlDocument();
            if (!File.Exists(file))
            {
                //创建类型声明节点
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
            }
            else
            {
                try
                {
                    xmlDoc.Load(file);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            //创建根节点
            XmlNode root = xmlDoc.SelectSingleNode("Settings");
            if (root == null)
            {
                root = xmlDoc.CreateElement("Settings");
                xmlDoc.AppendChild(root);
            }

            //LCR仪COM口设置
            XmlNode comNode = root.SelectSingleNode("LcrCom");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("LcrCom");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "19200");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位
            CreateNode(xmlDoc, comNode, "LcrType", "LCR11022");//仪器型号

            //LC仪COM口设置
            comNode = root.SelectSingleNode("LcCom");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("LcCom");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "9600");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位
            CreateNode(xmlDoc, comNode, "LcType", "CH2311");//仪器型号

            //卡尺COM口设置
            comNode = root.SelectSingleNode("CaliperCom");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("CaliperCom");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "9600");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位

            //10通道LC  LC2
            comNode = root.SelectSingleNode("LcTenChannelCom");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("LcTenChannelCom");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "9600");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位

            //通道盒子
            comNode = root.SelectSingleNode("BoxCom");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("BoxCom");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "9600");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位

            comNode = root.SelectSingleNode("BoxCom2");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("BoxCom2");
                root.AppendChild(comNode);
            }
            CreateNode(xmlDoc, comNode, "ComName", "COM1"); //串口
            CreateNode(xmlDoc, comNode, "StopBits", "1"); //停止位
            CreateNode(xmlDoc, comNode, "BaudRate", "9600");//波特率
            CreateNode(xmlDoc, comNode, "DataBits", "8");//数据位
            CreateNode(xmlDoc, comNode, "Parity", "无");//检验位

            //是否全自動測試
            comNode = root.SelectSingleNode("IsAllAuto");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("IsAllAuto");
                root.AppendChild(comNode);
            }

            //设备列表
            comNode = root.SelectSingleNode("Machines");
            if (comNode == null)
            {
                comNode = xmlDoc.CreateElement("Machines");
                root.AppendChild(comNode);
            }

            _XmlDocument = xmlDoc;
            xmlDoc.Save(file);
        }

        public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            if (parentNode.SelectSingleNode(name) != null) return;
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, string.Empty);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        public static string GetSettingFileName()
        {
            return Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory//System.Environment.CurrentDirectory
                , "Settings\\Settings.xml");
        }

        public static XmlNode GetXmlNode(string name)
        {
            XmlNode node = null;
            XmlDocument xmlDoc = GeteXmlDoc();

            node = xmlDoc.SelectSingleNode(name);
            if (node == null)
            {
                CreateSettingFile();
            }

            return node;
        }
        #endregion 配置文件XML

        #region COM口
        public static XmlNode GetLcrComXmlNode()
        {
            return Settings.GetXmlNode("/Settings/LcrCom");
        }
        public static XmlNode GetLcComXmlNode()
        {
            return Settings.GetXmlNode("/Settings/LcCom");
        }
        public static XmlNode GetCaliperComXmlNode()
        {
            return Settings.GetXmlNode("/Settings/CaliperCom");
        }
        public static XmlNode GetLc10ComXmlNode()
        {
            return Settings.GetXmlNode("/Settings/LcTenChannelCom");
        }
        public static XmlNode GetBoxComXmlNode()
        {
            return Settings.GetXmlNode("/Settings/BoxCom");
        }
        public static XmlNode GetBoxCom2XmlNode()
        {
            return Settings.GetXmlNode("/Settings/BoxCom2");
        }
        public static XmlNode GetIsAllAutoXmlNode()
        {
            return Settings.GetXmlNode("/Settings/IsAllAuto");
        }

        /// <summary>
        /// 是否全自動測試
        /// </summary>
        /// <returns></returns>
        public static bool IsAllAuto()
        {
            bool flag = false;
            XmlNode node = Settings.GetXmlNode("/Settings/IsAllAuto");
            if (node != null && !string.IsNullOrWhiteSpace(node.InnerText))
            {
                bool.TryParse(node.InnerText, out flag);
            }

            return flag;
        }

        public static SerialPort GetSerialPort(MachineType machineType, SerialPort serialPort)
        {
            XmlNode node = null;
            if (machineType == MachineType.LCR)
                node = Settings.GetLcrComXmlNode();
            else if (machineType == MachineType.LC)
                node = Settings.GetLcComXmlNode();
            else if (machineType == MachineType.Caliper)
                node = Settings.GetCaliperComXmlNode();
            else if (machineType == MachineType.Lc10)
                node = Settings.GetLc10ComXmlNode();
            else if (machineType == MachineType.Box)
                node = Settings.GetBoxComXmlNode();
            else if (machineType == MachineType.Box2)
                node = Settings.GetBoxCom2XmlNode();

            if (node != null)
            {
                serialPort.PortName = node.SelectSingleNode("ComName").InnerText;

                int baudRate = 9600;
                int.TryParse(node.SelectSingleNode("BaudRate").InnerText, out baudRate);
                serialPort.BaudRate = baudRate;

                int dataBits = 8;
                int.TryParse(node.SelectSingleNode("DataBits").InnerText, out dataBits);
                serialPort.DataBits = dataBits;

                string stopBitsString = node.SelectSingleNode("StopBits").InnerText;
                System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.Two;
                if (stopBitsString == "1") stopBits = System.IO.Ports.StopBits.One;
                else if (stopBitsString == "1.5") stopBits = System.IO.Ports.StopBits.OnePointFive;
                else if (stopBitsString == "2") stopBits = System.IO.Ports.StopBits.Two;
                else stopBits = System.IO.Ports.StopBits.None;
                serialPort.StopBits = stopBits;

                Parity parity = Parity.None;
                string parityString = node.SelectSingleNode("Parity").InnerText;
                if (parityString == "无" || parityString == "無") parity = Parity.None;
                else if (parityString == "奇校验" || parityString == "奇校驗") parity = Parity.Odd;
                else if (parityString == "偶校验" || parityString == "偶校驗") parity = Parity.Even;
                serialPort.Parity = parity;
            }

            return serialPort;
        }

        public static LcrType GetLcrType()
        {
            LcrType lcrType = LcrType.LCR11022;
            string code = string.Empty;

            XmlNode node = GetLcrComXmlNode();
            if (node != null && node.SelectSingleNode("LcrType") != null)
            {
                code = node.SelectSingleNode("LcrType").InnerText;
            }

            if (string.IsNullOrWhiteSpace(code))
                code = "LCR11022";

            Enum.TryParse(code, out lcrType);
            return lcrType;
        }
        public static LcType GetLcType()
        {
            string code = string.Empty;
            LcType lcType = LcType.CH2311;

            XmlNode node = GetLcComXmlNode();
            if (node != null && node.SelectSingleNode("LcType") != null)
            {
                code = node.SelectSingleNode("LcType").InnerText;
            }

            if (string.IsNullOrWhiteSpace(code))
                code = "CH2311";
            Enum.TryParse(code, out lcType);

            return lcType;
        }
        #endregion Com口

        public static XmlNode GetMachinesXmlNode()
        {
            return Settings.GetXmlNode("/Settings/Machines");
        }
        public static List<MachineSet> GetMachineSets()
        {
            List<MachineSet> ms = null;
            var node = GetMachinesXmlNode();
            if (node != null)
            {
                if (!string.IsNullOrWhiteSpace(node.InnerText))
                    ms = node.InnerText.ToObject<List<MachineSet>>();
            }

            if (ms == null) ms = new List<MachineSet>();

            return ms;
        }

        public static MachineSet GetMachineSet(MachineType machineType)
        {
            List<MachineSet> ms = null;
            MachineSet m = null;
            var node = GetMachinesXmlNode();
            if (node != null)
            {
                if (!string.IsNullOrWhiteSpace(node.InnerText))
                    ms = node.InnerText.ToObject<List<MachineSet>>();
            }

            string machineTypeName = Enum.GetName(typeof(MachineType), machineType);
            if (ms != null) m = ms.Where(t => t.MachineType == machineTypeName).FirstOrDefault();

            return m;
        }

        public enum MachineType
        {
            LCR,
            LC,
            /// <summary>
            /// 卡尺
            /// </summary>
            Caliper,
            /// <summary>
            /// 10通道LC
            /// </summary>
            Lc10,
            /// <summary>
            /// 通道盒子
            /// </summary>
            Box,
            /// <summary>
            /// 推拉力计
            /// </summary>
            Imada,
            /// <summary>
            /// 压力表
            /// </summary>
            PressureGauge,
            /// <summary>
            /// 硬度计
            /// </summary>
            Hardness,
            /// <summary>
            /// 三通道TV仪
            /// </summary>
            TV,

            /// <summary>
            /// 低电阻仪
            /// </summary>
            LowR,
            /// <summary>
            /// 高电阻仪
            /// </summary>
            HighR,
            /// <summary>
            /// PH计
            /// </summary>
            PH,
            /// <summary>
            /// 电导仪
            /// </summary>
            Conductivity,
            /// <summary>
            /// 单通道TV仪
            /// </summary>
            TV1,
            /// <summary>
            /// 两通道TV仪
            /// </summary>
            TV2,
            /// <summary>
            /// 导针折弯机
            /// </summary>
            Zhewan,
            /// <summary>
            /// 铝箔折弯机
            /// </summary>
            Puller,
            /// <summary>
            /// 击穿电压试验仪器
            /// </summary>
            BreakVoltage,
            /// <summary>
            /// 通道盒子2
            /// </summary>
            Box2,
            None = 999
        }
        public enum LcrType
        {
            /// <summary>
            /// Chroma 11022
            /// </summary>
            [Description("Chroma 11022")]
            LCR11022,
            /// <summary>
            /// 成華 CH-6021
            /// </summary>
            [Description("成华 CH-6021")]
            CH6021,
            /// <summary>
            /// 雙雄 SX10091
            /// </summary>
            [Description("双雄 SX10091")]
            SX10091A,
            /// <summary>
            /// "致新 ZX1085-1X
            /// </summary>
            [Description("致新 ZX1085-1X")]
            ZX1085_1X,
            /// <summary>
            /// 飞梦图 FE2826A
            /// </summary>
            [Description("飞梦图 FE2826A")]
            FE2826A,
            /// <summary>
            /// 飞梦图 FE2821
            /// </summary>
            [Description("飞梦图 FE2821")]
            FE2821,
            /// <summary>
            /// 飞梦图 FE2613B
            /// </summary>
            [Description("飞梦图 FE2613B")]
            FE2613B,
            /// <summary>
            /// 飞梦图 FE2613B
            /// </summary>
            [Description("飞梦图 FE2613")]
            FE2613,
            None
        }

        public enum LcType
        {
            /// <summary>
            /// 成華 CH-2311
            /// </summary>
            [Description("成华 CH-2311")]
            CH2311,
            /// <summary>
            /// 雙雄SX650X
            /// </summary>
            [Description("双雄 SX-1601/2.0")]
            SX650X,
            /// <summary>
            /// 致新 ZX6589
            /// </summary>
            [Description("致新 ZX6589")]
            ZX6589,
            /// <summary>
            /// 双雄 65002A 20通道LC
            /// </summary>
            [Description("双雄 65002A")]
            SX65002A,
            /// <summary>
            /// 同惠 TH2689A
            /// </summary>
            [Description("同惠 TH2689A")]
            TH2689A
                ,
            /// <summary>
            /// SX1601_20
            /// </summary>
            [Description("SX1601_20")]
            SX1601_20

        }
        public static string GetReportFileName(string path,string file,out string msg)
        {
            msg = string.Empty;
            string fileName = Path.Combine(path, file);
            if (string.IsNullOrWhiteSpace(file))
            {
                msg = "報表文件路徑為空，無法生成報表";
            }
            else if (!File.Exists(fileName))
            {
                string ss = Path.Combine(Environment.CurrentDirectory, "reports\\" +file);
                if (!File.Exists(ss))
                {
                    msg = "報表文件不存在：\r\n" + fileName + "\r\n" + ss;
                }
                else
                {
                    fileName = ss;
                }

            }

            return fileName;
        }
    }

    public class MachineSet
    {
        public string MachineType { get; set; } = string.Empty;
        public string PortName { get; set; } = string.Empty;
        //public int BaudRate { get { return int.Parse(BaudRateString); } }
        public int BaudRate { get; set; }
        public string IP { get; set; } = string.Empty;
        public string Remark { get; set; } = string.Empty;



        public static List<T> GetMachineType<T>() where T : new()
        {
            List<T> _MachineTypes = new List<T>();

            var propertyCode = typeof(T).GetProperty("Code");
            var propertyName = typeof(T).GetProperty("Name");

            if (propertyCode == null || propertyName == null)
                throw new Exception("传入的泛型类型必须有属性Code和Name");

            T set = new T();
            propertyCode.SetValue(set, "PressureGauge", null);
            propertyName.SetValue(set, "压力表", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "Imada", null);
            propertyName.SetValue(set, "推拉力计", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "Hardness", null);
            propertyName.SetValue(set, "硬度计", null);
            _MachineTypes.Add(set);

            //
            set = new T();
            propertyCode.SetValue(set, "LowR", null);
            propertyName.SetValue(set, "低阻仪", null);
            _MachineTypes.Add(set);

            //
            set = new T();
            propertyCode.SetValue(set, "HighR", null);
            propertyName.SetValue(set, "绝缘电阻仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "TV", null);
            propertyName.SetValue(set, "三通道TV仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "TV2", null);
            propertyName.SetValue(set, "两通道TV仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "TV1", null);
            propertyName.SetValue(set, "单通道TV仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "PH", null);
            propertyName.SetValue(set, "PH仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "Conductivity", null);
            propertyName.SetValue(set, "电导仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "BreakVoltage", null);
            propertyName.SetValue(set, "击穿电压试验仪", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "Zhewan", null);
            propertyName.SetValue(set, "导针折弯机", null);
            _MachineTypes.Add(set);

            set = new T();
            propertyCode.SetValue(set, "Puller", null);
            propertyName.SetValue(set, "铝箔折弯机", null);
            _MachineTypes.Add(set);

            return _MachineTypes;
        }
        public static List<T> GetBaudRate<T>() where T : new()
        {
            List<T> BaudRates = new List<T>();

            var propertyCode = typeof(T).GetProperty("Id");
            var propertyName = typeof(T).GetProperty("Name");

            if (propertyCode == null || propertyName == null)
                throw new Exception("传入的泛型类型必须有属性Id和Name");

            T set = new T();
            propertyCode.SetValue(set, 9600, null);
            propertyName.SetValue(set, "9600", null);
            BaudRates.Add(set);

            set = new T();
            propertyCode.SetValue(set, 19200, null);
            propertyName.SetValue(set, "19200", null);
            BaudRates.Add(set);

            set = new T();
            propertyCode.SetValue(set, 14400, null);
            propertyName.SetValue(set, "14400", null);
            BaudRates.Add(set);

            //
            set = new T();
            propertyCode.SetValue(set, 38400, null);
            propertyName.SetValue(set, "38400", null);
            BaudRates.Add(set);

            //
            set = new T();
            propertyCode.SetValue(set, 115200, null);
            propertyName.SetValue(set, "115200", null);
            BaudRates.Add(set);



            //
            set = new T();
            propertyCode.SetValue(set, 4800, null);
            propertyName.SetValue(set, "4800", null);
            BaudRates.Add(set);

            set = new T();
            propertyCode.SetValue(set, 2400, null);
            propertyName.SetValue(set, "2400", null);
            BaudRates.Add(set);

            return BaudRates;
        }
    }
}
